//
//  SubViewController.m
//  WCycleScrollView
//
//  Created by winter on 16/7/24.
//  Copyright © 2016年 d. All rights reserved.
//

#import "SubViewController.h"
#import "WCarouselScrollView.h"

@interface WCarouselCollectionCell : UICollectionViewCell
@property (nonatomic, copy) NSString *text;
@property (nonatomic, weak) UILabel *label;
@end
@implementation WCarouselCollectionCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UILabel *label = [[UILabel alloc] initWithFrame:self.bounds];
        label.font = [UIFont systemFontOfSize:25];
        label.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:label];
        self.label = label;
    }
    return self;
}

- (void)setText:(NSString *)text
{
    _text = text;
    self.label.text = text;
}
@end

@interface CellMoel : NSObject
@property (nonatomic, strong) UIColor *color;
@property (nonatomic, copy) NSString *text;
@end
@implementation CellMoel
@end

@interface SubViewController ()<WCarouselScrollViewDelegate,WCarouselScrollViewDataSource>
@property (nonatomic, strong) WCarouselScrollView *scrollView;
@property (nonatomic, strong) NSArray *dataArray;
@end

@implementation SubViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self fakeData];
    
    NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"h4" ofType:@"jpg"];
    
    CGRect frame = CGRectMake(0, 100, self.view.bounds.size.width, 180);
    self.scrollView = [[WCarouselScrollView alloc] initWithFrame:frame itemSize:CGSizeMake(280, 180)];
    self.scrollView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:imagePath]];
    self.scrollView.dataSource = self;
    self.scrollView.delegate = self;
    [self.scrollView registerClass:[WCarouselCollectionCell class] forCellWithReuseIdentifier:@"cell"];
    [self.view addSubview:self.scrollView];
}

- (UIColor *)randomColor
{
    CGFloat hue = (arc4random() % 256 / 256.0 ); //0.0 to 1.0
    CGFloat saturation = (arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0,away from white
    CGFloat brightness = (arc4random() % 128 / 256.0 ) + 0.5; //0.5 to 1.0,away from black
    return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
}

- (void)fakeData
{
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];
    for (int i = 0; i < 10; i++) {
        CellMoel *model = [[CellMoel alloc] init];
        model.color = [self randomColor];
        model.text = [NSString stringWithFormat:@"%d",i];
        [array addObject:model];
    }
    self.dataArray = array;
}

#pragma mark - WCarouselScrollView Delegate / DataSource

- (NSInteger)numberOfItemsForCarouselScrollView
{
    return self.dataArray.count;
}

- (UICollectionViewCell *)carouselScrollView:(WCarouselScrollView *)wCarouselView cellForItemAtIndex:(NSInteger)index
{    
    WCarouselCollectionCell *cell= [wCarouselView dequeueReusableCellWithReuseIdentifier:@"cell" forIndex:index];
    CellMoel *model = self.dataArray[index];
    cell.backgroundColor = model.color;
    cell.text = model.text;
    return cell;
}

- (void)carouselScrollView:(WCarouselScrollView *)wCarouselView didSelectItemAtIndex:(NSInteger)index
{
    NSLog(@"clicked index = %ld",index);
}

@end
